package com.zh.chat.entity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class UserInfo extends User {
	public static int ORDER_BY_DIVIDNAME=1;
	public static int ORDER_ID=2;
	public static int ORDER_LASTLOGIN_DATE=3;
	
	private Map<Long, Friend> friendsMap = new HashMap<Long, Friend>();
	
	
	public UserInfo() {
		super();
		// TODO Auto-generated constructor stub
	}
	public UserInfo(String userPwd, String nickname, Date registerDate,
			Date lastLoginDate, Integer status, Integer clientPort,
			Integer clientFilePort, String clientIp, String image) {
		super(userPwd, nickname, registerDate, lastLoginDate, status, clientPort,
				clientFilePort, clientIp, image);
		// TODO Auto-generated constructor stub
	}

	/**
	 * 根据参数对集合进行排序<br/>
	 * 1:根据分组名称排序<br/>
	 * 2:根据用户的ID进行排序<br/>
	 * 3:根据上次登录时间进行排序<br/>
	 * @param orderBy
	 * @return
	 */
	public ArrayList<Friend> getFriends(int orderBy){
		ArrayList<Friend> list = new ArrayList<Friend>();
		Iterator<Long> it = friendsMap.keySet().iterator();
		while (it.hasNext()) {
			Long key = it.next();
            list.add(friendsMap.get(key));
		}
		switch (orderBy) {
		case 1: Collections.sort(list,new orderByDividName());break;
		case 2: Collections.sort(list,new orderById());break;
		case 3: Collections.sort(list,new orderByLastLoginDate());break;
		default:
			break;
		}
		return list;
	}
	
	public Friend getFriend(java.lang.Long id){
		return friendsMap.get(id);
	}
	
	public void initUser(User user){
		setSysId(user.getSysId());					//用户名
		setUserPwd(user.getUserPwd());				//密码MD5值
		setNickname(user.getNickname());			//昵称
		setName(user.getName());					//姓名
		setRegisterDate(user.getRegisterDate());	//注册日期
		setLastLoginDate(user.getLastLoginDate());	//上次登录时间
		setStatus(user.getStatus());				//状态
		setClientPort(user.getClientPort());		//客户端端口
		setClientFilePort(user.getClientFilePort());//客户端文件端口
		setClientIp(user.getClientIp());			//客户端IP
		setBirthday(user.getBirthday());			//生日 
		setSex(user.getSex());						//姓别
		setImage(user.getImage());					//头像
	}
	
	/**
	 * 1.发送添加好友指令给服务器<br/>
	 * 2.接收服务器返回的数据，并根据接收到的数据做出不同的反应<br/>
	 * 3.从列表中删除好友，更新界面<br/>
	 * @param friend
	 */
	public void addFriend(Friend friend) {
		//TODO 发送添加好友指令给服务器
		friendsMap.put(friend.getFriendId(), friend);
	}
	
	/**
	 * 1.发送删除好友指令给服务器<br/>
	 * 2.接收服务器返回的数据，并根据接收到的数据做出不同的反应<br/>
	 * 3.从列表中删除好友，更新界面<br/>
	 * @param friendId
	 */
	public void delFriend(Long friendId) {
		friendsMap.remove(friendId);
	}
	public User getUser() {
		User user = new User();
		user.setSysId(super.getSysId());
		user.setUserPwd(super.getUserPwd());
		user.setSex(super.getSex());
		user.setName(super.getName());
		user.setNickname(super.getNickname());
		user.setImage(super.getImage());
		user.setLastLoginDate(super.getLastLoginDate());
		user.setRegisterDate(super.getRegisterDate());
		user.setStatus(super.getStatus());
		user.setBirthday(super.getBirthday());
		return user;
	}
}

class orderById implements Comparator<Friend> {
	public int compare(Friend f1, Friend f2) {
		return (int) (f1.getSysId() - f2.getSysId());
	}
}
class orderByDividName implements Comparator<Friend> {
	public int compare(Friend f1, Friend f2) {
		return (int) (f1.getDivideName().hashCode()- f2.getDivideName().hashCode());
	}
}
class orderByLastLoginDate implements Comparator<Friend>{
	public int compare(Friend o1, Friend o2) {
		long time = o1.getLastLoginDate()-o2.getLastLoginDate();
		return (int)time;
	}
}